
import{getProductslist,getEvents} from '../../api/store.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    parameter: {
      'navbar': '1',
      'return': '1',
      'title': ''
    },
    tab:1,
    page:1,
    limit:10,
    list:[],
    loading:false,
    star_id:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      star_id:options.id,
      parameter: {
        'navbar': '1',
        'return': '1',
        'title': options.title,
      }
    })
    this.getList()
  },
  getList(){
    let that = this
    if(this.data.loading||this.data.loadend){
      return
    }
    this.setData({
      loading:true,
    })
    let req
    if(that.data.tab==3){
      req = getEvents({page:this.data.page,limit:this.data.limit,source:that.data.tab,star_id:that.data.star_id})
    }else{
      req = getProductslist({page:this.data.page,limit:this.data.limit,source:that.data.tab,star_id:that.data.star_id})
    }
    req.then(res=>{
      var list = that.data.list;
      var loadend = list.length < that.data.limit;
      if (that.data.page == 1) {
        list = res.data.data
      } else {
        list.push(...res.data)
      }
        that.setData({
          list:list,
          loadend:loadend,
          loading:false,
          page: that.data.page + 1,
        })
    })
  },
  
  onTabChange(v) {
    const tab = v.currentTarget.dataset.tab
    if (tab != this.data.tab) {
      this.setData({
        tab: tab,
        page:1,
        loadend:false,
        list:[]
      })
      this.getList()
    }
  },
  onItemClick(e){
   let item =  e.currentTarget.dataset.item
   if(this.data.tab==3){
    wx.navigateTo({ url: `/pages/event_detail/index?id=${item.product_id}&acId=${item.id}&public_date=${item.public_date}&state=${item.state}` });
   }else{
     wx.navigateTo({
       url: `/pages/goods_details_new/index?id=${item.id}`,
     })
   }
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getList()
  },


})